package com.eascs.web.o2o.scheduler.task;


import javax.inject.Inject;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.classify.entity.O2oLoanAbjustType;
import com.eascs.admin.classify.service.IO2oClassifyService;
import com.eascs.admin.common.service.IArchiveService;
import com.eascs.admin.common.service.ICreditArchiveService;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.admin.o2o.badloan.service.ICancelService;
import com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService;
import com.eascs.admin.o2o.submitted.service.IO2oChargeSituationService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.web.o2o.quartz.util.BaseJobUtil;
import com.eascs.web.o2o.scheduler.entity.CreditManagerEntity;
import com.eascs.web.o2o.scheduler.service.SynchroAfterLoanForService;
import com.eascs.web.o2o.scheduler.service.SynchroService;
import com.eascs.web.o2o.scheduler.service.SynoInfoAssetStructureBjService;
import com.eascs.web.o2o.scheduler.service.SynoInfoAssetStructureService;

/**
      admin：与admin相关的定时任务，并且数据落库在admin中
 */
@Component("AdminSynchroTask")
public class AdminSynchroTask extends BaseJobUtil{
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Reference
	IArchiveService iArchiveService;
	@Reference
	ICreditArchiveService creditArchiveService;
	@Inject 
	SynchroAfterLoanForService synchroAfterLoanForService;
	@Inject
	SynoInfoAssetStructureService synoInfoAssetStructureService;
	@Inject
	SynchroService		synchroService;
	@Inject
	SynoInfoAssetStructureBjService synoInfoAssetStructureBjService;
	@Reference
	IO2oClassifyService iO2oClassifyService;
	@Reference
	ICancelService iCancelService;
	@Reference
	IO2oChargeSituationService iO2oChargeSituationService;
	@Reference
	IO2oCollectFeesService iO2oCollectFeesService;
	
	// 归档定时
	@Scheduled(cron = "0 0 0 * * ?")
	public void archive() {
		logger.info("[workflow] [归档0点]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			com.eascs.admin.common.vo.ModelResult<Boolean> result = iArchiveService.buildArchiveExport();
			logger.info("异常原因：[{}]", result.getErrMsg());
		} catch (Exception e) {
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[workflow] [归档0点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}

	/**
	 * 征信归档
	 */
	//@Scheduled(cron = "0 0 2 * * ?")
	public void creditArchive() {
		logger.info("[workflow] [征信归档2点]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			creditArchiveService.startup();
		} catch (Exception e) {
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[workflow] [征信归档2点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
	
	
	/**
	 * 新征信归档
	 */
	@Scheduled(cron = "0 0 3 * * ?")
	public void creditNewArchive() {
		logger.info("[workflow] [征信归档3点]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			creditArchiveService.startupNew();
		} catch (Exception e) {
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[workflow] [征信归档3点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
	
	/**
	 * admin 数据归档
	 * 2016年10月19日
	 */
	@Scheduled(cron = "0 0 5 * * ?")
	public void onFile() {
		logger.info("admin 数据归档任务开始......");
		StopWatch stopWatch = new StopWatch();
		ModelResult<Boolean> result = iArchiveService.startUpPlaceOnFile();
		long stop = stopWatch.elapsedTime() / 60000;
		if(result.isSuccess()){
			boolean flag = result.getModel();
			if(flag){
				logger.info("[admin] [数据归档]任务[完成]，共耗时：[{}]分", stop);
			} else{
				logger.info("[admin] [数据归档]任务[失败]，共耗时：[{}]分", stop);
			}
		} else{
			logger.info("[admin] [数据归档]任务[异常]，错误信息[{}],共耗时：[{}]分", result.getErrMsg(), stop);
		}
	}
	

	/**
	 *更新征信归档状态
	 */
	//@Scheduled(cron = "0 0 2 * * ?")
	public void updateCreditStatus() {
		logger.info("service数据抽取任务开始......");
		StopWatch stopWatch = new StopWatch();
			synchroService.updateCreditStatus();
		logger.info("service数据抽取任务结束，共耗时：[{}]分", stopWatch.elapsedTime() / 60000);
	}
	
	

	/**
	 * 
	 * @Title: creditManagerRankReport
	 * @Description: (省区和信贷经理的关联关系)
	 * @author user
	 * @return void
	 * @throws
	 * @date 2017年4月12日 下午2:32:07
	 */
	@Scheduled(cron = "0 00 00 3 * ?")
	public void creditManagerRankReport() {
		logger.info("省区和信贷经理的关联关系任务开始......");
			synchroService.loadWToS();
		logger.info("省区和信贷经理的关联关系任务结束......");
	}
	
	
	@Scheduled(cron = "0 00 4 * * ?")
	public void saveO2oPlatformRegional() {
		logger.info("用户中心平台大区数据同步任务开始......");
		StopWatch stopWatch = new StopWatch();
			synchroService.saveO2oPlatformRegional2();
		logger.info("用户中心平台大区数据同步任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
	}
	
	
	//===============================================================================================
	
	/**
	 * 
	 * @Title: syncForService
	 * @Description: (正常余额,上年年末余额，第一次放款时间,续贷)：
	 * 正常余额：还没有还给银行的本金（还款中的，本金逾期的）
	 * 全部余额=正常余额+逾期本金+逾期利息+罚息
	 * 逾期/已到期但是未做逾期处理（未上传逾期报表）/还款中====取“放款金额”作为正常余额
	 * @author user
	 * @return void
	 * @throws
	 * @date 2017年4月12日 下午2:32:07
	 */
	@Scheduled(cron = "0 0/40 * * * ?")
	public void syncForService() {
		StopWatch s=new StopWatch();
		logger.info("同步放款余额到service开始");
		try {
			synchroAfterLoanForService.synchroAfterLoanForBillCode();
		} catch (Exception e) {
			logger.info("同步放款余额异常[{}]",e.getMessage());
		}
		logger.info("同步放款余额到service结束，耗时：[{}]秒",s.elapsedTime()/1000);
		
		StopWatch s1=new StopWatch();
		logger.info("同步放款次数，第一次放款时间到service开始");
		try {
			synchroAfterLoanForService.synchroAfterLoanForCustomer();
		} catch (Exception e) {
			logger.info("同步放款次数，第一次放款时间异常，[{}]",e.getMessage());
		}
		logger.info("同步放款次数，第一次放款时间到service结束，耗时：[{}]秒",s1.elapsedTime()/1000);
		
		StopWatch s2=new StopWatch();
		logger.info("开始实时更新放款余额历史存档中当天的数据");
		try {
			synchroAfterLoanForService.synchBalanceHisToday();
		} catch (Exception e) {
			logger.info("更新放款余额历史存档中当天的数据异常，[{}]",e.getMessage());
		}
		logger.info("结束实时更新放款余额历史存档中当天的数据，耗时：[{}]秒",s2.elapsedTime()/1000);
		
		StopWatch s3=new StopWatch();
		logger.info("开始同步续贷字段");
		try {
			synchroAfterLoanForService.synchReLoan();
		} catch (Exception e) {
			logger.info("同步续贷字段异常，[{}]",e.getMessage());
		}
		logger.info("结束同步续贷字段，耗时：[{}]秒",s3.elapsedTime()/1000);
	}
	

	
	/**
	 * 
	 * @Title: synchBalanceHisTask
	 * @Description:  向o2o_balance_his表中保存放款余额，本金逾期余额，代偿金额，当前代偿欠还余额。
	 * @author user
	 * @return void
	 * @throws
	 * @date 2017年4月13日 下午2:32:07
	 */
	@Scheduled(cron = "0 0 1 * * ?")
	public void synchBalanceHisTask(){
		StopWatch s1=new StopWatch();
		logger.info("同步余额数据，历史存档开始");
		synchroAfterLoanForService.synchBalanceHis();
		logger.info("同步余额数据，历史存档结束，耗时：[{}]秒",s1.elapsedTime()/1000);
	}
	
	
	//代偿余额同步
	@Scheduled(cron = "0 0 4 * * ?")
	public void updateCompensatory() {
		try {
			synchroAfterLoanForService.updateCompensatory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
		
	//保存资产结构本金日报表数据（报表中间数据表）
	@Scheduled(cron = "0 20 6 * * ?")
	public void saveAssetStructurePrincipalDaily() {
		try {
			synoInfoAssetStructureBjService.saveAssetStructurePrincipalDaily();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
//	===============================================================================================	
	
	
	
	
	
	
	
	/**
	 *将oa中的数据字典同步到admin (oa系统的数据同步全部关掉)
	 */
	//@Scheduled(cron = "0 0 1 * * ?")
	public void saveServiceJob() {
		logger.info("service数据抽取任务开始......");
		StopWatch stopWatch = new StopWatch();
		synchroService.saveData();
		logger.info("service数据抽取任务结束，共耗时：[{}]分", stopWatch.elapsedTime() / 60000);
	}
	
	
	
	//贷款申请超时作废
		@Scheduled(cron = "0 00 6 * * ?")
		public void cancelLoanapply() {
			try {
				synchroService.cancelLoanapply();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	//信贷经理放款清单（同步wofkflow中的信贷经理数据到service库中）
	@Scheduled(cron = "0 20 5 * * ?")
	public void creditManager() {
		logger.info("信贷经理放款清单同步任务开始......");
		StopWatch stopWatch = new StopWatch();
		try {
			synchroService.creditManager(CreditManagerEntity.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("信贷经理放款清单同步任务结束，共耗时：[{}]秒", stopWatch.elapsedTime() / 1000);
	}
	
	/**
	 * 自动更新贷款分类
	 */
	@Scheduled(cron = "0 10 2 * * ?")
	public void classifyAu() {
		logger.info("[service] [自动更新贷款分类2点]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			iO2oClassifyService.insertAU(new O2oLoanAbjustType());
		} catch (Exception e) {
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[service] [自动更新贷款分类2点]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
	
	/**
	 * loan-12269 新建状态单据系统自动作废 
	 */
	@Scheduled(cron = "0 20 2 * * ?")
	public void loanNewCancle() {
		logger.info("[service] [新建状态单据系统自动作废2点20分]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			ModelResult<Boolean> result = iCancelService.loanNewCancle();
			logger.info("是否成功：【{}】，若不成功错误信息：【{}】", result.isSuccess(), result.getErrMsg());
		} catch (Exception e) {
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[service] [新建状态单据系统自动作废2点20分]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
	
	/**
	 * loan-13099 单据驳回5个工作日内自动作废
	 */
	@Scheduled(cron = "0 6 1 * * ?")
	public void cancelBHLoanapply(){
		logger.info("[service] [贷款驳回超时5工作日作废开始]任务[开始]......");
		StopWatch time = new StopWatch();
		try {
			synchroService.cancelBHLoanapply();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.info("异常原因：[{}]", e.getMessage());
		}
		logger.info("[service] [贷款驳回超时5工作日作废]任务[结束]......耗时：【{}】分", time.elapsedTime() / 1000 / 60);
	}
}
